package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.MyOrderAdapter;
import com.example.lanouhn.firstapp.activity.model.CartList;
import com.example.lanouhn.firstapp.activity.utils.CartListJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

import java.util.List;

//我的订单列表的页面跳转
public class MyOrdersActivity extends AppCompatActivity {


    //关于我们--跳转
    private TextView tv_aboutOur;
    //免责声明
    private TextView tv_disclaimer;
    //商务合作
    private TextView tv_cooperate;
    //帮助中心
    private TextView tv_help;
    //注销
    private TextView tv_cancellation;
    //返回
    private ImageView iv_back;
    //个人中心的跳转
    private TextView mineCenter;
    //弹出标题栏
    private LinearLayout ll_title;
    private ImageView iv_title;
    private boolean isImageView = true;

    //隐藏标题栏的跳转
    private RelativeLayout rll_homePage;
    private RelativeLayout rll_classify;
    private RelativeLayout rll_shoppingCart;
    private RelativeLayout rll_myStore;


    //用户登录标识
    private String key;
    private String goods_id;
    private RecyclerView recyclerView;
    //数据集合
    private List<CartList> dataList;
    private MyOrderAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        key = (String) SPUtils.get(MyOrdersActivity.this,"log_key","");

        //找到控件
        initView();
        //监听事件
        initEvent();
        getDataFromWeb();
    }


    private void initView() {

        mineCenter = (TextView) findViewById(R.id.mineCenter);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        //绑定控件
        tv_aboutOur = (TextView) findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) findViewById(R.id.mine_cooperate);
        tv_help = (TextView) findViewById(R.id.mine_help);
        tv_cancellation = (TextView) findViewById(R.id.mine_cancellation);

        rll_homePage = (RelativeLayout) findViewById(R.id.rl_homePage);
        rll_classify = (RelativeLayout) findViewById(R.id.rl_classify);
        rll_shoppingCart = (RelativeLayout) findViewById(R.id.rl_shoppingCart);
        rll_myStore = (RelativeLayout) findViewById(R.id.rl_myStore);

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_title = (ImageView) findViewById(R.id.iv_title);

    }


    private void initEvent() {
       //绑定控件
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //加载局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });

        //注销--点击事件
        tv_cancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });


        //个人中心--点击事件
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, MainActivity.class);
                intent.putExtra("symbol", 4);
                startActivity(intent);
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                setResult(101,intent);
                finish();


            }
        });
        //弹出标题栏监听事件
        iv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isImageView) {
                    ll_title.setVisibility(View.VISIBLE);
                    isImageView = false;
                } else {
                    ll_title.setVisibility(View.GONE);
                    isImageView = true;

                }
            }
        });
        //标题栏首页按钮监听事件
        rll_homePage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, MainActivity.class);
                intent.putExtra("symbol", 1);
                startActivity(intent);
            }
        });
        //标题栏分类按钮监听事件
        rll_classify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, MainActivity.class);
                intent.putExtra("symbol", 2);
                startActivity(intent);
            }
        });
        //标题栏购物车按钮监听事件
        rll_shoppingCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, MainActivity.class);
                intent.putExtra("symbol", 3);
                startActivity(intent);
            }
        });
        //标题栏我的商城按钮监听事件
        rll_myStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyOrdersActivity.this, MainActivity.class);
                intent.putExtra("symbol", 4);
                startActivity(intent);
            }
        });
    }



    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    break;
                case 1:
                    //创建adapter
                    adapter  = new MyOrderAdapter(MyOrdersActivity.this,dataList);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    break;
                default:
                    break;


            }


        }
    };
    private void getDataFromWeb() {
        //开启线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://www.niaolema.com/mobile/index.php?act=member_cart&op=cart_list";
                String params = "key=" + key;
                String result = HttpUtils.doPost(url,params);
                if(!result.equals("") && !result.equals("null")){
                    dataList = CartListJson.getCartList(result);

                    if(dataList.size()>0){
                        //解析成功
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }


                }else {
                    handler.sendEmptyMessage(0);

                }


            }
        }).start();



    }

}

